/**
 * 
 */
package com.epart.crypt;

/**
 * @author cwsong
 * @since 2012. 6. 11.
 */
public class Utils {
	
	static byte[] stringToByteArray(String digits, int radix){
		if (digits == null) {
			return null;
		}
		if (radix != 16 && radix != 10 && radix != 8) {
			throw new IllegalArgumentException("For input radix: \"" + radix + "\"");
		}
		int divLen = (radix == 16) ? 2 : 3;
		int length = digits.length();
		if (length % divLen == 1) {
			throw new IllegalArgumentException("For input string: \"" + digits + "\"");
		}
		length = length / divLen;
		byte[] bytes = new byte[length];
		for (int i = 0; i < length; i++) {
			int index = i * divLen;
			bytes[i] = (byte) (Short.parseShort(digits.substring(index, index + divLen), radix));
		}
		return bytes;
	}
	
	static String byteArrayToHexString(byte[] bytes){
		if (bytes == null) {
			return null;
		}

		StringBuffer result = new StringBuffer();
		for (int i = 0; i < 16; i++) {
			result.append(Integer.toString((bytes[i] & 0xF0) >> 4, 16));
			result.append(Integer.toString(bytes[i] & 0x0F, 16));
		}
		return result.toString();
	}
	
}
